<template>
  <div class="panel admin-panel">
    <div class="panel-head" id="add">
      <strong>
        <span class="icon-pencil-square-o"></span>增加职位
      </strong>
    </div>
    <div class="body-content">
      <form method="post" class="form-x" action onsubmit="return false">
      
        <div class="form-group">
          <!-- 名称 -->
          <div class="label">
            <label>职位名称：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" v-model="cate.name" ref="cateName"/>
            <div class="tips">{{msg}}</div>
          </div>
          <br />
          <br />
          <br />
          <!-- 父id -->
          <div class="label">
            <label>父类：</label>
          </div>
          <div class="field">
            <category-chooser :categories="categories" @changeCate="changeCate"></category-chooser>
            <div class="tips"></div>
          </div>
          <br />
          <br />
          <br />
          <!-- 首页展示 -->
          <!-- <div class="label">
            <label>首页显示：</label>
          </div>
          <div class="field">
            <el-radio-group v-model="cate.showIndex">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
            <div class="tips"></div>
          </div> -->

        </div>

        <div class="form-group">
          <div class="label">
            <label></label>
          </div>
          <div class="field">
            <button class="button bg-main icon-check-square-o" @click="send">提交</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import CategoryChooser from "./CategoryChooser"
export default {
  name: "CategoryAdd",
  components:{
    CategoryChooser
  },
  props: {
    categories: Array,
    add: Function // 添加到本地视图
  },
  data() {
    return {
      cate: {
        name: "",
        parentId: null,
        // showIndex: 1
      },
      msg:""
    };
  },
  methods: {
    //改变选中类别
    changeCate(cid){
      this.cate.parentId=cid;
    },
    //发送更改信息
    send() {
      if(this.cate.name.length<1){
          this.msg="类别名不为空！";
          this.$refs.cateName.focus();
          return;
      }
      axios
        .post(this.host + "/category/add", this.cate)
        .then(r => {
          this.add(r.data.data);
          this.suc("保存成功!");
        })
        .catch(r => {
          this.error("保存出错!");
        });
    }
  }
}
</script>

<style scoped>
</style>
